<?php
/**
 * Create By akon
 * date: 2016年12月3日23:22:31
 * 后台公共方法封装
 */
/**打印函数**/
function p($var)
{
    echo "<pre>";
    print_r($var);
    echo "</pre>";
}

/**密码加密**/
function encryptPass($pass)
{
    $password = sha1(md5($pass));
    return $password;
}

/**encrypt解密**/
function decryptPass($res)
{
    //RSA私钥
    $private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDAbfx4VggVVpcfCjzQ+nEiJ2DLnRg3e2QdDf/m/qMvtqXi4xhw
vbpHfaX46CzQznU8l9NJtF28pTSZSKnE/791MJfVnucVcJcxRAEcpPprb8X3hfdx
KEEYjOPAuVseewmO5cM+x7zi9FWbZ89uOp5sxjMnlVjDaIczKTRx+7vn2wIDAQAB
AoGAZzUWajxKTZeJqh5FjBgmwZi5M7voFynZAjRWAkCkqZye0FfY7e70kA92C1AL
aVqySnNr4WYZuGorEeOFGqHIv1XSowTLgfLkVBZ/SXiep2QYJrR0YevjysvLnTfb
mrdWCqWSj+0AlQg+AvDA/qtvBVMxKymbpo+4bj5H2pPPZ1ECQQDi1PwJQJBYPbpL
vGmP3AmWg467tCeQ+aJGgtQTOK5BH+p0BWFVDX583R437vllkKI8EXgZfqQfsQcj
7XUAXyZVAkEA2SyFbO8roH9JLrEoxxKGeiGZvhPfNl9nXLhX0OFS0ywQaVBJno39
9W5bX5iP5Jzeb3UWsZ/TxzhGc/b4WjAlbwJBAOFuIn1feRT5Y+hY++BJIg4/+N57
EMd4ENpas0HXFvcKLQvZPP42Rvr5FksoaRuTPmjMQ7uyrJICccI3AAy6g3ECQQDE
AyH9+zRmLNxRj0advsOvUcpgu7DYc21oS12/Qs+tl3TMiNGZkNDphwxjkOA217sP
4B92fCn6AnncSslHJXNzAkBo6ujxqIfrZMOG3ON9nXxkWlq39GFS6CzXWscHA3Xz
FMVT1WWU3FR2Kf2QSKiMGv02YcI2xfowim3JnT6600N0
-----END RSA PRIVATE KEY-----';
    openssl_private_decrypt(base64_decode($res), $decrypted, $private_key);
    return json_decode($decrypted, true);
}

/**管理员操作日志
 * @param $login_info
 * @param $login_url
 */
function adminLogin($login_info, $login_url)
{
    $info = array(
        "login_ip" => getIP(),
        "login_time" => time(),
        "login_info" => $login_info,
        "login_url" => $login_url,
        "admin_id" => session("admin_id"),
    );
    M("admin_log")->add($info);
}

/** 定义一个函数getIP() 客户端IP，**/
function getIP()
{
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";
    return $ip;
}

/**服务器端IP**/
function serverIP()
{
    return gethostbyname($_SERVER["SERVER_NAME"]);
}

/**无限极分类
 * @param $cate 所有数据
 * @param string $html
 * @param int $pid 父级分类id
 * @param int $level 分类等级
 */
function unlimitedForLevel($cate, $html = '&nbsp;&nbsp|----', $pid = 0, $level = 0)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['parent_id'] == $pid) {
            //级别
            $v['level'] = $level + 1;
            //填充属性符号
            $v['html'] = str_repeat($html, $level);
            $arr[] = $v;
            //递归
            $arr = array_merge($arr, unlimitedForLevel($cate, $html, $v['id'], $level + 1));
        }
    }
    return $arr;
}